
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>generator · JavaScript学习</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Veng">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="async.html" />
    
    
    <link rel="prev" href="../浏览器/Promise.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础.html">
            
                <a href="基础.html">
            
                    
                    基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="变量.html">
            
                <a href="变量.html">
            
                    
                    数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="let和const命令.html">
            
                <a href="let和const命令.html">
            
                    
                    let和const命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="数值.html">
            
                <a href="数值.html">
            
                    
                    数值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="对象.html">
            
                <a href="对象.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="数组.html">
            
                <a href="数组.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="Symbol.html">
            
                <a href="Symbol.html">
            
                    
                    Symbol
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.8.1" data-path="函数的扩展.html">
            
                <a href="函数的扩展.html">
            
                    
                    函数的扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.2" data-path="变量的解构赋值.html">
            
                <a href="变量的解构赋值.html">
            
                    
                    变量作用域与解构赋值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.3" data-path="函数3.html">
            
                <a href="函数3.html">
            
                    
                    方法函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.4" data-path="高阶函数.html">
            
                <a href="高阶函数.html">
            
                    
                    高阶函数
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.8.4.1" data-path="map-reduce.html">
            
                <a href="map-reduce.html">
            
                    
                    map/reduce
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.4.2" data-path="filter.html">
            
                <a href="filter.html">
            
                    
                    filter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.4.3" data-path="sort.html">
            
                <a href="sort.html">
            
                    
                    sort
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.4.4" data-path="array其他方法.html">
            
                <a href="array其他方法.html">
            
                    
                    array其他方法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.8.5" data-path="闭包.html">
            
                <a href="闭包.html">
            
                    
                    闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.6" data-path="箭头函数.html">
            
                <a href="箭头函数.html">
            
                    
                    箭头函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.7" data-path="../浏览器/Promise.html">
            
                <a href="../浏览器/Promise.html">
            
                    
                    Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.8.8" data-path="generator.html">
            
                <a href="generator.html">
            
                    
                    generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.9" data-path="async.html">
            
                <a href="async.html">
            
                    
                    async
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="标准对象.html">
            
                <a href="标准对象.html">
            
                    
                    标准对象
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.9.1" data-path="SetMap.html">
            
                <a href="SetMap.html">
            
                    
                    Set,Map
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.2" data-path="Date.html">
            
                <a href="Date.html">
            
                    
                    Date
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.3" data-path="Math.html">
            
                <a href="Math.html">
            
                    
                    Math
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.4" data-path="Proxy.html">
            
                <a href="Proxy.html">
            
                    
                    Proxy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.5" data-path="RepExp.html">
            
                <a href="RepExp.html">
            
                    
                    正则表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.6" data-path="正则表达式.html">
            
                <a href="正则表达式.html">
            
                    
                    正则表达式别篇
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.7" data-path="json.html">
            
                <a href="json.html">
            
                    
                    JSON
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="面向对象编程.html">
            
                <a href="面向对象编程.html">
            
                    
                    面向对象编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.10.1" data-path="对象的扩展.html">
            
                <a href="对象的扩展.html">
            
                    
                    对象的扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10.2" data-path="创建对象.html">
            
                <a href="创建对象.html">
            
                    
                    创建对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10.3" data-path="原型继承.html">
            
                <a href="原型继承.html">
            
                    
                    原型继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10.4" data-path="class继承.html">
            
                <a href="class继承.html">
            
                    
                    class继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10.5" data-path="模块化Module.html">
            
                <a href="模块化Module.html">
            
                    
                    模块化Module
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="编程风格.html">
            
                <a href="编程风格.html">
            
                    
                    编程风格
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../js扩展/js扩展.html">
            
                <a href="../js扩展/js扩展.html">
            
                    
                    js 扩展
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../js扩展/字符串.html">
            
                <a href="../js扩展/字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../浏览器/浏览器.html">
            
                <a href="../浏览器/浏览器.html">
            
                    
                    浏览器
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../浏览器/浏览器对象.html">
            
                <a href="../浏览器/浏览器对象.html">
            
                    
                    浏览器对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../浏览器/操作DOM.html">
            
                <a href="../浏览器/操作DOM.html">
            
                    
                    操作DOM
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.2.1" data-path="../浏览器/更新DOM.html">
            
                <a href="../浏览器/更新DOM.html">
            
                    
                    更新DOM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.2" data-path="../浏览器/插入DOM.html">
            
                <a href="../浏览器/插入DOM.html">
            
                    
                    插入DOM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.3" data-path="../浏览器/删除DOM.html">
            
                <a href="../浏览器/删除DOM.html">
            
                    
                    删除DOM
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../浏览器/DOM事件.html">
            
                <a href="../浏览器/DOM事件.html">
            
                    
                    DOM事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../浏览器/操作表单.html">
            
                <a href="../浏览器/操作表单.html">
            
                    
                    操作表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../浏览器/操作文件.html">
            
                <a href="../浏览器/操作文件.html">
            
                    
                    操作文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../浏览器/AJAX.html">
            
                <a href="../浏览器/AJAX.html">
            
                    
                    AJAX
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../浏览器/Canvas.html">
            
                <a href="../浏览器/Canvas.html">
            
                    
                    Canvas
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../Jquery/jquery.html">
            
                <a href="../Jquery/jquery.html">
            
                    
                    Jquery
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../Jquery/选择器.html">
            
                <a href="../Jquery/选择器.html">
            
                    
                    选择器
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1.1" data-path="../Jquery/层级选择器.html">
            
                <a href="../Jquery/层级选择器.html">
            
                    
                    层级选择器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.2" data-path="../Jquery/查找和过滤.html">
            
                <a href="../Jquery/查找和过滤.html">
            
                    
                    查找和过滤
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../Jquery/操作DOM.html">
            
                <a href="../Jquery/操作DOM.html">
            
                    
                    操作DOM
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.2.1" data-path="../Jquery/修改DOM结构.html">
            
                <a href="../Jquery/修改DOM结构.html">
            
                    
                    修改DOM结构
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../Jquery/事件.html">
            
                <a href="../Jquery/事件.html">
            
                    
                    事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../Jquery/动画.html">
            
                <a href="../Jquery/动画.html">
            
                    
                    动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../Jquery/AJAX.html">
            
                <a href="../Jquery/AJAX.html">
            
                    
                    AJAX
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../Jquery/扩展.html">
            
                <a href="../Jquery/扩展.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../nodejs/nodejs.html">
            
                <a href="../nodejs/nodejs.html">
            
                    
                    Nodejs
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../nodejs/模块.html">
            
                <a href="../nodejs/模块.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../nodejs/path.html">
            
                <a href="../nodejs/path.html">
            
                    
                    path
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../nodejs/文件操作.html">
            
                <a href="../nodejs/文件操作.html">
            
                    
                    文件操作 fs
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../nodejs/流Stream.html">
            
                <a href="../nodejs/流Stream.html">
            
                    
                    流Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../nodejs/zlib压缩解压缩.html">
            
                <a href="../nodejs/zlib压缩解压缩.html">
            
                    
                    zlib压缩解压缩
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../nodejs/net网络.html">
            
                <a href="../nodejs/net网络.html">
            
                    
                    net网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../nodejs/http.html">
            
                <a href="../nodejs/http.html">
            
                    
                    http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../nodejs/express框架.html">
            
                <a href="../nodejs/express框架.html">
            
                    
                    express框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../nodejs/process进程.html">
            
                <a href="../nodejs/process进程.html">
            
                    
                    process 进程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../nodejs/child_process.html">
            
                <a href="../nodejs/child_process.html">
            
                    
                    child_process
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.11" data-path="../nodejs/os.html">
            
                <a href="../nodejs/os.html">
            
                    
                    os-操作系统
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../react/react.html">
            
                <a href="../react/react.html">
            
                    
                    React
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >generator</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="generator">generator</h1>
<p>generator&#xFF08;&#x751F;&#x6210;&#x5668;&#xFF09;&#x662F;ES6&#x6807;&#x51C6;&#x5F15;&#x5165;&#x7684;&#x65B0;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x3002;&#x4E00;&#x4E2A;generator&#x770B;&#x4E0A;&#x53BB;&#x50CF;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x4F46;&#x53EF;&#x4EE5;&#x8FD4;&#x56DE;&#x591A;&#x6B21;&#x3002;</p>
<p>&#x51FD;&#x6570;&#x5728;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x9047;&#x5230;<code>return</code>&#x8BED;&#x53E5;&#xFF08;&#x51FD;&#x6570;&#x672B;&#x5C3E;&#x5982;&#x679C;&#x6CA1;&#x6709;<code>return</code>&#xFF0C;&#x5C31;&#x662F;&#x9690;&#x542B;&#x7684;<code>return undefined;</code>&#xFF09;&#xFF0C;&#x63A7;&#x5236;&#x6743;&#x65E0;&#x6CD5;&#x4EA4;&#x56DE;&#x88AB;&#x8C03;&#x7528;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<p>generator&#x548C;&#x51FD;&#x6570;&#x4E0D;&#x540C;&#x7684;&#x662F;&#xFF0C;generator&#x7531;<code>function*</code>&#x5B9A;&#x4E49;&#xFF08;&#x6CE8;&#x610F;&#x591A;&#x51FA;&#x7684;<code>*</code>&#x53F7;&#xFF09;&#xFF0C;&#x5E76;&#x4E14;&#xFF0C;&#x9664;&#x4E86;<code>return</code>&#x8BED;&#x53E5;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x7528;<code>yield</code>&#x8FD4;&#x56DE;&#x591A;&#x6B21;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x4EE5;&#x4E00;&#x4E2A;&#x8457;&#x540D;&#x7684;&#x6590;&#x6CE2;&#x90A3;&#x5951;&#x6570;&#x5217;&#x4E3A;&#x4F8B;&#xFF0C;&#x5B83;&#x7531;<code>0</code>&#xFF0C;<code>1</code>&#x5F00;&#x5934;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">fib</span>(<span class="hljs-params">max</span>) </span>{
    <span class="hljs-keyword">var</span>
        t,
        a = <span class="hljs-number">0</span>,
        b = <span class="hljs-number">1</span>,
        n = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">while</span> (n &lt; max) {
        <span class="hljs-keyword">yield</span> a;
        [a, b] = [b, a + b];
        n ++;
    }
    <span class="hljs-keyword">return</span>;
}
</code></pre>
<p>&#x76F4;&#x63A5;&#x8C03;&#x7528;&#x4E00;&#x4E2A;generator&#x548C;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x4E0D;&#x4E00;&#x6837;&#xFF0C;<code>fib(5)</code>&#x4EC5;&#x4EC5;&#x662F;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;generator&#x5BF9;&#x8C61;&#xFF0C;&#x8FD8;&#x6CA1;&#x6709;&#x53BB;&#x6267;&#x884C;&#x5B83;&#x3002;</p>
<p>&#x8C03;&#x7528;generator&#x5BF9;&#x8C61;&#x6709;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x4E00;&#x662F;&#x4E0D;&#x65AD;&#x5730;&#x8C03;&#x7528;generator&#x5BF9;&#x8C61;&#x7684;<code>next()</code>&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> f = fib(<span class="hljs-number">5</span>);
f.next(); <span class="hljs-comment">// {value: 0, done: false}</span>
f.next(); <span class="hljs-comment">// {value: 1, done: false}</span>
f.next(); <span class="hljs-comment">// {value: 1, done: false}</span>
f.next(); <span class="hljs-comment">// {value: 2, done: false}</span>
f.next(); <span class="hljs-comment">// {value: 3, done: false}</span>
f.next(); <span class="hljs-comment">// {value: undefined, done: true}</span>
</code></pre>
<p>&#x8981;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x81EA;&#x589E;&#x7684;ID&#xFF0C;&#x53EF;&#x4EE5;&#x7F16;&#x5199;&#x4E00;&#x4E2A;<code>next_id()</code>&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">nextId</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> currentId = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>){
        <span class="hljs-keyword">yield</span> currentId+=<span class="hljs-number">1</span>;
    }
}
<span class="hljs-keyword">const</span> f = nextId();
<span class="hljs-built_in">console</span>.log(f.next());
<span class="hljs-built_in">console</span>.log(f.next());
</code></pre>
<h3 id="generator-&#x51FD;&#x6570;&#x7684;&#x5F02;&#x6B65;&#x5E94;&#x7528;">Generator &#x51FD;&#x6570;&#x7684;&#x5F02;&#x6B65;&#x5E94;&#x7528;</h3>
<p>&#x5F02;&#x6B65;&#x7F16;&#x7A0B;&#x5BF9; JavaScript &#x8BED;&#x8A00;&#x592A;&#x91CD;&#x8981;&#x3002;JavaScript &#x8BED;&#x8A00;&#x7684;&#x6267;&#x884C;&#x73AF;&#x5883;&#x662F;&#x201C;&#x5355;&#x7EBF;&#x7A0B;&#x201D;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x5F02;&#x6B65;&#x7F16;&#x7A0B;&#xFF0C;&#x6839;&#x672C;&#x6CA1;&#x6CD5;&#x7528;&#xFF0C;&#x975E;&#x5361;&#x6B7B;&#x4E0D;&#x53EF;&#x3002;&#x672C;&#x7AE0;&#x4E3B;&#x8981;&#x4ECB;&#x7ECD; Generator &#x51FD;&#x6570;&#x5982;&#x4F55;&#x5B8C;&#x6210;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x3002;</p>
<h3 id="&#x534F;&#x7A0B;">&#x534F;&#x7A0B;</h3>
<p>&#x4F20;&#x7EDF;&#x7684;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#xFF0C;&#x65E9;&#x6709;&#x5F02;&#x6B65;&#x7F16;&#x7A0B;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#xFF08;&#x5176;&#x5B9E;&#x662F;&#x591A;&#x4EFB;&#x52A1;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#xFF09;&#x3002;&#x5176;&#x4E2D;&#x6709;&#x4E00;&#x79CD;&#x53EB;&#x505A;&quot;&#x534F;&#x7A0B;&quot;&#xFF08;coroutine&#xFF09;&#xFF0C;&#x610F;&#x601D;&#x662F;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E92;&#x76F8;&#x534F;&#x4F5C;&#xFF0C;&#x5B8C;&#x6210;&#x5F02;&#x6B65;&#x4EFB;&#x52A1;&#x3002;</p>
<p>&#x534F;&#x7A0B;&#x6709;&#x70B9;&#x50CF;&#x51FD;&#x6570;&#xFF0C;&#x53C8;&#x6709;&#x70B9;&#x50CF;&#x7EBF;&#x7A0B;&#x3002;&#x5B83;&#x7684;&#x8FD0;&#x884C;&#x6D41;&#x7A0B;&#x5927;&#x81F4;&#x5982;&#x4E0B;&#x3002;</p>
<ul>
<li>&#x7B2C;&#x4E00;&#x6B65;&#xFF0C;&#x534F;&#x7A0B;<code>A</code>&#x5F00;&#x59CB;&#x6267;&#x884C;&#x3002;</li>
<li>&#x7B2C;&#x4E8C;&#x6B65;&#xFF0C;&#x534F;&#x7A0B;<code>A</code>&#x6267;&#x884C;&#x5230;&#x4E00;&#x534A;&#xFF0C;&#x8FDB;&#x5165;&#x6682;&#x505C;&#xFF0C;&#x6267;&#x884C;&#x6743;&#x8F6C;&#x79FB;&#x5230;&#x534F;&#x7A0B;<code>B</code>&#x3002;</li>
<li>&#x7B2C;&#x4E09;&#x6B65;&#xFF0C;&#xFF08;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x540E;&#xFF09;&#x534F;&#x7A0B;<code>B</code>&#x4EA4;&#x8FD8;&#x6267;&#x884C;&#x6743;&#x3002;</li>
<li>&#x7B2C;&#x56DB;&#x6B65;&#xFF0C;&#x534F;&#x7A0B;<code>A</code>&#x6062;&#x590D;&#x6267;&#x884C;&#x3002;</li>
</ul>
<p>&#x4E0A;&#x9762;&#x6D41;&#x7A0B;&#x7684;&#x534F;&#x7A0B;<code>A</code>&#xFF0C;&#x5C31;&#x662F;&#x5F02;&#x6B65;&#x4EFB;&#x52A1;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x5206;&#x6210;&#x4E24;&#x6BB5;&#xFF08;&#x6216;&#x591A;&#x6BB5;&#xFF09;&#x6267;&#x884C;&#x3002;</p>
<p>&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x7684;&#x534F;&#x7A0B;&#x5199;&#x6CD5;&#x5982;&#x4E0B;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">readFile</span>(<span class="hljs-params"></span>) </span>{
    setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;readFile&quot;</span>);
    },<span class="hljs-number">1000</span>);
}
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">asyncJob</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;start&quot;</span>);
    <span class="hljs-keyword">yield</span> readFile();
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;end&quot;</span>);
}
<span class="hljs-keyword">const</span> job = asyncJob();
job.next();
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;out&quot;</span>);

<span class="hljs-comment">/**
start
out
readFile
*/</span>
</code></pre>
<p>&#x901A;&#x8FC7;&#x8FD9;&#x4E2A;&#x53EF;&#x4EE5;&#x4E86;&#x89E3;&#x5230;&#x534F;&#x7A0B;&#x662F;&#x975E;&#x963B;&#x585E;&#x7684;&#xFF0C;&#x5B83;&#x5C06;&#x4EFB;&#x52A1;&#x4EA4;&#x7ED9;&#x4E86;&#x53E6;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5904;&#x7406;&#xFF0C;&#x4E3B;&#x7EBF;&#x7A0B;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x3002;</p>
<h3 id="co&#x6A21;&#x5757;">co&#x6A21;&#x5757;</h3>
<p><a href="https://github.com/tj/co" target="_blank">co &#x6A21;&#x5757;</a>&#x662F;&#x8457;&#x540D;&#x7A0B;&#x5E8F;&#x5458; TJ Holowaychuk &#x4E8E; 2013 &#x5E74; 6 &#x6708;&#x53D1;&#x5E03;&#x7684;&#x4E00;&#x4E2A;&#x5C0F;&#x5DE5;&#x5177;&#xFF0C;&#x7528;&#x4E8E; Generator &#x51FD;&#x6570;&#x7684;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A; Generator &#x51FD;&#x6570;&#xFF0C;&#x7528;&#x4E8E;&#x4F9D;&#x6B21;&#x8BFB;&#x53D6;&#x4E24;&#x4E2A;&#x6587;&#x4EF6;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> gen = <span class="hljs-function"><span class="hljs-keyword">function</span>* (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">var</span> f1 = <span class="hljs-keyword">yield</span> readFile(<span class="hljs-string">&apos;/etc/fstab&apos;</span>);
  <span class="hljs-keyword">var</span> f2 = <span class="hljs-keyword">yield</span> readFile(<span class="hljs-string">&apos;/etc/shells&apos;</span>);
  <span class="hljs-built_in">console</span>.log(f1.toString());
  <span class="hljs-built_in">console</span>.log(f2.toString());
};
</code></pre>
<p>co &#x6A21;&#x5757;&#x53EF;&#x4EE5;&#x8BA9;&#x4F60;&#x4E0D;&#x7528;&#x7F16;&#x5199; Generator &#x51FD;&#x6570;&#x7684;&#x6267;&#x884C;&#x5668;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> co = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;co&apos;</span>);
co(gen);
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;Generator &#x51FD;&#x6570;&#x53EA;&#x8981;&#x4F20;&#x5165;<code>co</code>&#x51FD;&#x6570;&#xFF0C;&#x5C31;&#x4F1A;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x3002;</p>
<p><code>co</code>&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;<code>Promise</code>&#x5BF9;&#x8C61;&#xFF0C;&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x7528;<code>then</code>&#x65B9;&#x6CD5;&#x6DFB;&#x52A0;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;</p>
<pre><code class="lang-javascript">co(gen).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Generator &#x51FD;&#x6570;&#x6267;&#x884C;&#x5B8C;&#x6210;&apos;</span>);
});
</code></pre>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x7B49;&#x5230; Generator &#x51FD;&#x6570;&#x6267;&#x884C;&#x7ED3;&#x675F;&#xFF0C;&#x5C31;&#x4F1A;&#x8F93;&#x51FA;&#x4E00;&#x884C;&#x63D0;&#x793A;&#x3002;</p>
<h3 id="&#x5904;&#x7406;&#x5E76;&#x53D1;&#x7684;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;">&#x5904;&#x7406;&#x5E76;&#x53D1;&#x7684;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;</h3>
<p>co &#x652F;&#x6301;&#x5E76;&#x53D1;&#x7684;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#xFF0C;&#x5373;&#x5141;&#x8BB8;&#x67D0;&#x4E9B;&#x64CD;&#x4F5C;&#x540C;&#x65F6;&#x8FDB;&#x884C;&#xFF0C;&#x7B49;&#x5230;&#x5B83;&#x4EEC;&#x5168;&#x90E8;&#x5B8C;&#x6210;&#xFF0C;&#x624D;&#x8FDB;&#x884C;&#x4E0B;&#x4E00;&#x6B65;&#x3002;</p>
<p>&#x8FD9;&#x65F6;&#xFF0C;&#x8981;&#x628A;&#x5E76;&#x53D1;&#x7684;&#x64CD;&#x4F5C;&#x90FD;&#x653E;&#x5728;&#x6570;&#x7EC4;&#x6216;&#x5BF9;&#x8C61;&#x91CC;&#x9762;&#xFF0C;&#x8DDF;&#x5728;<code>yield</code>&#x8BED;&#x53E5;&#x540E;&#x9762;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x6570;&#x7EC4;&#x7684;&#x5199;&#x6CD5;</span>
co(<span class="hljs-function"><span class="hljs-keyword">function</span>* (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">var</span> res = <span class="hljs-keyword">yield</span> [
    <span class="hljs-built_in">Promise</span>.resolve(<span class="hljs-number">1</span>),
    <span class="hljs-built_in">Promise</span>.resolve(<span class="hljs-number">2</span>)
  ];
  <span class="hljs-built_in">console</span>.log(res);
}).catch(onerror);

<span class="hljs-comment">// &#x5BF9;&#x8C61;&#x7684;&#x5199;&#x6CD5;</span>
co(<span class="hljs-function"><span class="hljs-keyword">function</span>* (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">var</span> res = <span class="hljs-keyword">yield</span> {
    <span class="hljs-number">1</span>: <span class="hljs-built_in">Promise</span>.resolve(<span class="hljs-number">1</span>),
    <span class="hljs-number">2</span>: <span class="hljs-built_in">Promise</span>.resolve(<span class="hljs-number">2</span>),
  };
  <span class="hljs-built_in">console</span>.log(res);
}).catch(onerror);
</code></pre>
<p>&#x4E0B;&#x9762;&#x662F;&#x53E6;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;</p>
<pre><code class="lang-javascript">co(<span class="hljs-function"><span class="hljs-keyword">function</span>* (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">var</span> values = [n1, n2, n3];
  <span class="hljs-keyword">yield</span> values.map(somethingAsync);
});

<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">somethingAsync</span>(<span class="hljs-params">x</span>) </span>{
  <span class="hljs-comment">// do something async</span>
  <span class="hljs-keyword">return</span> y
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x5141;&#x8BB8;&#x5E76;&#x53D1;&#x4E09;&#x4E2A;<code>somethingAsync</code>&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#xFF0C;&#x7B49;&#x5230;&#x5B83;&#x4EEC;&#x5168;&#x90E8;&#x5B8C;&#x6210;&#xFF0C;&#x624D;&#x4F1A;&#x8FDB;&#x884C;&#x4E0B;&#x4E00;&#x6B65;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../浏览器/Promise.html" class="navigation navigation-prev " aria-label="Previous page: Promise">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="async.html" class="navigation navigation-next " aria-label="Next page: async">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"generator","level":"1.2.8.8","depth":3,"next":{"title":"async","level":"1.2.8.9","depth":3,"path":"基础/async.md","ref":"基础/async.md","articles":[]},"previous":{"title":"Promise","level":"1.2.8.7","depth":3,"path":"浏览器/Promise.md","ref":"浏览器/Promise.md","articles":[]},"dir":"ltr"},"config":{"plugins":["lunr","pageview-count","chapter-fold","back-to-top-button","code","hide-element","-sharing","sharing-plus","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"chapter-fold":{},"search":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"lunr":{"ignoreSpecialCharacters":false,"maxIndexSize":1000000},"code":{"copyButtons":true},"hide-element":{"elements":[".gitbook-link"]},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"favicon":"js.ico","back-to-top-button":{},"pageview-count":{},"custom-favicon":{},"sharing":{"qq":true,"all":["douban","facebook","google","instapaper","linkedin","twitter","weibo","messenger","qq","qzone","viber","whatsapp"],"douban":true,"facebook":true,"weibo":true,"twitter":true,"pocket":true,"google":true,"qzone":true},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Veng","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"JavaScript学习","language":"zh-hans","gitbook":"*","description":"javaScript的学习笔记"},"file":{"path":"基础/generator.md","mtime":"2020-12-10T02:10:04.990Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-12-10T03:00:51.663Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

